package com.software.levelupandroid.taskShow.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.ImageButton;
import android.widget.PopupMenu;
import android.widget.TextView;
import android.widget.ToggleButton;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.software.levelupandroid.R;
import com.software.levelupandroid.entity.Task;
import com.software.levelupandroid.home.ui.home.HomeViewModel;
import com.software.levelupandroid.taskIssue.view.activity.TaskIssueActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description 任务展示的Adapter
 * @Author LiangYaJie
 * @CreateTime 2024年05月25日 17:57:58
 */
public class TaskShowAdapter extends RecyclerView.Adapter<TaskShowAdapter.ViewHolder> {

    private List<Task> tasks = new ArrayList<>();
    private Context context;

    public TaskShowAdapter(Context context, List<Task> tasks) {
        this.context = context;
        this.tasks = tasks;
    }

    @NonNull
    @Override
    public TaskShowAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_task_show, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull TaskShowAdapter.ViewHolder holder, int position) {
        Task task = tasks.get(position);

        //设置创建时间
        holder.tvCreatedAt.setText(task.getCreatedAt());

        //设置任务标题
        holder.tvTaskShowTitle.setText(task.getTaskTitle());

        //设置任务类型
        if (task.getTaskType() == 0){
            holder.tvTaskShowTaskType.setText("待办");
        }

        //设置任务优先级
        switch (task.getPriority()){
            case 0:
                holder.tvTaskShowPriority.setText("不重要不紧急");
                break;
            case 1:
                holder.tvTaskShowPriority.setText("重要不紧急");
                break;
            case 2:
                holder.tvTaskShowPriority.setText("不重要紧急");
                break;
            case 3:
                holder.tvTaskShowPriority.setText("重要且紧急");
                break;
        }

        //设置每个item的点击事件
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, TaskIssueActivity.class);
                intent.putExtra("task", task);
                intent.putExtra("flag", 1);
                context.startActivity(intent);
            }
        });

    }

    @Override
    public int getItemCount() {
        return tasks.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private TextView tvCreatedAt;
        private TextView tvTaskShowTitle;
        private ImageButton ibTaskShowMore;
        private TextView tvTaskShowTaskType;
        private TextView tvTaskShowPriority;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tvCreatedAt = itemView.findViewById(R.id.tv_taskShow_createdAt);
            tvTaskShowTitle = itemView.findViewById(R.id.tv_taskShow_taskTitle);
            ibTaskShowMore = itemView.findViewById(R.id.ib_taskShow_more);
            tvTaskShowTaskType = itemView.findViewById(R.id.tv_taskShow_taskType);
            tvTaskShowPriority = itemView.findViewById(R.id.tv_taskShow_priority);
        }
    }

    /**
     * 更新任务列表
     * @param tasks
     */
    public void updateTaskList(List<Task> tasks){
        this.tasks.clear();
        this.tasks.addAll(tasks);
        notifyDataSetChanged();
    }

    /**
     * 任务列表新增
     * @param tasks
     */
    public void addTaskList(List<Task> tasks){
        this.tasks.addAll(tasks);
        notifyDataSetChanged();
    }
}
